﻿using Microsoft.EntityFrameworkCore;
using star_questionnair.Utilities;

namespace star_questionnair.Services.impl;

public class TimeService : ITimeService
{
    private readonly ApplicationDbContext _dbContext;

    // 构造函数注入 DbContext
    public TimeService(ApplicationDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    // 平均完成时间
    public async Task<double> AverageFinishedTimeAsync(int questionnaireId)
    {
        // 从数据库获取所有响应记录
        var responses = await _dbContext.Responses
            .Where(r => r.QuestionnaireId == questionnaireId)
            .ToListAsync();

        if (responses == null || !responses.Any())
        {
            return 0.0;
        }

        double sum = 0.0;
        foreach (var response in responses)
        {
            if (response.SubmittedTime.HasValue)
            {
                // 计算从开始到提交的时间差，单位为分钟
                var duration = response.SubmittedTime.Value - response.StartTime;
                sum += duration.TotalMinutes; // 转换为分钟
            }
        }

        return sum / responses.Count();
    }

    // 每日时间段分析
    public async Task<Dictionary<string, int>> GetResponsesByTimePeriodAsync(int questionnaireId)
    {
        // 从数据库获取所有响应记录
        var responses = await _dbContext.Responses
            .Where(r => r.QuestionnaireId == questionnaireId && r.SubmittedTime.HasValue)
            .ToListAsync();

        var periodCountMap = new Dictionary<string, int>();

        foreach (var response in responses)
        {
            // 获取响应的开始时间并计算时间段
            string period = GetTimePeriod(response.StartTime);
            if (periodCountMap.ContainsKey(period))
            {
                periodCountMap[period]++;
            }
            else
            {
                periodCountMap[period] = 1;
            }
        }

        return periodCountMap;
    }

    // 获取时间段
    private string GetTimePeriod(DateTime startTime)
    {
        int hour = startTime.Hour;

        if (hour >= 0 && hour < 6)
        {
            return "00:00-06:00";
        }
        else if (hour >= 6 && hour < 12)
        {
            return "06:00-12:00";
        }
        else if (hour >= 12 && hour < 18)
        {
            return "12:00-18:00";
        }
        else
        {
            return "18:00-00:00";
        }
    }

    // 每周时间分析
    public async Task<Dictionary<string, int>> GetResponsesByDayOfWeekAsync(int questionnaireId)
    {
        // 从数据库获取所有响应记录
        var responses = await _dbContext.Responses
            .Where(r => r.QuestionnaireId == questionnaireId && r.SubmittedTime.HasValue)
            .ToListAsync();

        var dayCountMap = new Dictionary<string, int>();

        foreach (var response in responses)
        {
            // 获取响应的提交时间，并计算星期几
            string dayOfWeek = GetDayOfWeek(response.SubmittedTime.Value);
            if (dayCountMap.ContainsKey(dayOfWeek))
            {
                dayCountMap[dayOfWeek]++;
            }
            else
            {
                dayCountMap[dayOfWeek] = 1;
            }
        }

        return dayCountMap;
    }

    // 获取星期几
    private string GetDayOfWeek(DateTime submittedTime)
    {
        switch (submittedTime.DayOfWeek)
        {
            case DayOfWeek.Monday:
                return "Monday";
            case DayOfWeek.Tuesday:
                return "Tuesday";
            case DayOfWeek.Wednesday:
                return "Wednesday";
            case DayOfWeek.Thursday:
                return "Thursday";
            case DayOfWeek.Friday:
                return "Friday";
            case DayOfWeek.Saturday:
                return "Saturday";
            case DayOfWeek.Sunday:
                return "Sunday";
            default:
                return "Unknown";
        }
    }
}